﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cartoon.Model;
using Belyn.Common.Web;
using System.Text.RegularExpressions;
using Cartoon.Common;

namespace Cartoon.Logic
{
    public class CartoonPresenter
    {
        protected ICartoonView iView;
        protected ICartoonService iService;

        public CartoonPresenter(ICartoonView iView, ICartoonService iService)
        {
            this.iView = iView;
            this.iService = iService;
            this.initEvent();
        }

        public CartoonPresenter(ICartoonView iView)
        {
            this.iView = iView;
            this.iService = new CartoonViewerService();
            this.initEvent();
        }

        protected void initEvent()
        {
            this.iView.EventSaveImage += new SaveImageDelegate(iView_OnSaveImage);
            this.iView.EventGetChapterList += new GetChapterListDelegate(iView_OnGetChapterList);
        }

        protected ChapterList iView_OnGetChapterList(Comic comic)
        {
            return this.GetChapterList(comic);
        }

        protected void iView_OnSaveImage(string imgUrl, string savePath)
        {
            this.iService.SaveImage(imgUrl, this.iView.Domain, savePath);
        }

        public string GetChapterListFilePath(Comic comic)
        {
            return this.iView.CartoonSavePath + "\\" + comic.Name + "\\ChapterList.xml";
        }

        protected ChapterList GetChapterList(Comic comic)
        {
            string comicFilePath = this.GetChapterListFilePath(comic);
            ChapterList chpList = ChapterList.Load(comicFilePath, comic);
            int maxExistChapterID = chpList.MaxChapterID;

            string url = comic.Url;
            string htmlcode = "";
            bool isGetServerChapterList = false;
            try
            {
                htmlcode = HttpRequest.GetHttpCode(url);
                isGetServerChapterList = true;
            }
            catch (Exception ex)
            {
                isGetServerChapterList = false;
                Log.debug(ex.StackTrace + "\r\n获取漫画 《" + comic.Name + "》的最新集数出错 ：" + ex.Message);
                //throw new Exception("获取漫画 《" + comic.Name + "》的集数出错 ：" + ex.Message);
            }
            bool haveNewChapter = false;
            if (isGetServerChapterList == true)
            {
                Regex rg = new Regex(comic.RegexExpForChapter);
                MatchCollection mCollection = rg.Matches(htmlcode);

                foreach (Match m_01 in mCollection)
                {
                    Chapter chp = new Chapter()
                    {
                        Id = int.Parse(m_01.Groups[2].Value),
                        Src = comic.ChapterDomain + m_01.Groups[1].Value,
                        comic = comic
                    };

                    if (chp.Id > maxExistChapterID)
                    {
                        haveNewChapter = true;
                        chpList.Add(chp);
                    }
                }

                try
                {
                    if (haveNewChapter == true)
                    {
                        //有新集数，保存下来
                        chpList.Save(comicFilePath);
                    }
                }
                catch (Exception ignore) { }
            }

            return chpList;

        }

    }
}
